﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using SdmxMl.Common;

namespace SdmxMl.Registry
{
    /// <summary>
    /// This carries the text of error messages and/or warnings in response
    /// to queries or requests. The Status attribute carries the staus of
    /// the query or request.
    /// </summary>
    public class StatusMessageType
    {
        private StatusType _status;
        public StatusType Status
        {
            get { return _status; }
            set { _status = value; }
        }
        
        /// <summary>Optionnal message</summary>
        private MultiLang _message;
        public MultiLang Message
        {
            get { return _message; }
            set { _message = value; }
        }

        public StatusMessageType()
        {
            _status = StatusType.Success;
            Message = new MultiLang();
            _isStatusMessage = true;
        }

        private bool _isStatusMessage;
        public bool IsStatusMessage
        {
            get { return _isStatusMessage; }
        }

        public StatusMessageType(XmlElement el)
        {
            _status = StatusType.Failure;
            _isStatusMessage = false;
            if (el != null && el.FirstChild != null && el.FirstChild.Name == "StatusMessage")
            {
                _isStatusMessage = true;
                string str = el.FirstChild.Attributes["status"].Value;
                if (Enum.IsDefined(typeof(StatusType), str))
                    _status = (StatusType)Enum.Parse(typeof(StatusType), str, true);
                else
                    throw new ApplicationException("Unrecognised Status Value: " + str);

                if (el.FirstChild.FirstChild != null && el.FirstChild.FirstChild.Name == "MessageText")
                    Message = new MultiLang(el.FirstChild.FirstChild.InnerText);
            }
        }

    }

    public class RegistryMessageType
    {
        private StatusType _status;
        public StatusType Status
        {
            get { return _status; }
            set { _status = value; }
        }

        /// <summary>Optionnal message</summary>
        private MultiLang _message;
        public MultiLang Message
        {
            get { return _message; }
            set { _message = value; }
        }

        public RegistryMessageType()
        {
            _status = StatusType.Success;
            Message = new MultiLang();
            _isStatusMessage = true;
        }

        private bool _isStatusMessage;
        public bool IsStatusMessage
        {
            get { return _isStatusMessage; }
        }

        public RegistryMessageType(XmlElement el)
        {
            _status = StatusType.Failure;
            _isStatusMessage = false;
            if (el != null && el.Name == "Registry")
            {
                _isStatusMessage = true;
                string str = el.FirstChild.Attributes["status"].Value;
                if (str == "success")
                    _status = StatusType.Success;
                else if (str == "warning")
                    _status = StatusType.Warning;
                else if (str == "failure")
                    _status = StatusType.Failure;
                else
                    throw new ApplicationException("Unrecognised Status Value: " + str);

                if (el.FirstChild.FirstChild != null && el.FirstChild.FirstChild.Name == "Message")
                    Message = new MultiLang(el.FirstChild.FirstChild.InnerText);
            }
        }

    }


}
